package com.zho.common.utility.db;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

public class QueryBuilderUtils {

	public static String getQueryString(String queryString, String groupBy, String orderColumn, List<SearchItem> restrictions) {
		final StringBuffer bufWhere = new StringBuffer();

		if (!CollectionUtils.isEmpty(restrictions)) {
			for (SearchItem item : restrictions) {
				if ((item.getExpression() != null && StringUtils.isNotBlank(String.valueOf(item.getExpression())))) {
					bufWhere.append(" " + item.getLogicOperator() + " " + item.getExpression() + " ");
				}
			}
		}

		if (bufWhere.length() > 0) {
			bufWhere.replace(0, 4, "WHERE");
		}

		final StringBuffer bufGroupBy = new StringBuffer();
		if (StringUtils.isNotBlank(groupBy)) {
			bufGroupBy.append(" group by " + groupBy);
		}
		
		final StringBuffer bufOrder = new StringBuffer();
		if (StringUtils.isNotBlank(orderColumn)) {
			bufOrder.append(" order by " + orderColumn);
		}

		return queryString.toString() + bufWhere + bufGroupBy + bufOrder;
	}

	/**
	 * Get Query and Pass parameters
	 * 
	 * @param	queryString: query string
	 * 			orderColumn: order column
	 * 			restrictions: key, value
	 * @return	NamedQuery
	 * @auth ho.viet.duan
	 * @CrDate Dec 27, 2010
	 * @MdDate
	 */
	public static Query getQuery(Session em, String queryString, String groupBy, String orderColumn, List<SearchItem> restrictions) {
		String generatedQueryString = getQueryString(queryString, groupBy, orderColumn, restrictions);
		final HashMap<String, Object> hashMap = new HashMap<String, Object>();

		if (!CollectionUtils.isEmpty(restrictions)) {
			for (SearchItem item : restrictions) {
				if ((item.getValue() != null && StringUtils.isNotBlank(String.valueOf(item.getValue())))) {
					hashMap.put(item.getKey(), item.getValue());
				}
			}
		}

		Query query = em.createQuery(generatedQueryString);
		for (String key : hashMap.keySet()) {
			if (hashMap.get(key) instanceof List) {
				query.setParameterList(key, (List)hashMap.get(key));
			} else {
				query.setParameter(key, hashMap.get(key));
			}
		}
		
		return query;
	}
	
	/**
	 * Get NamedQuery and Pass parameters
	 * 
	 * @param	namedQueryString: query key
	 * 			restrictions: key, value
	 * @return	NamedQuery
	 * @auth ho.viet.duan
	 * @CrDate Dec 27, 2010
	 * @MdDate
	 */
	public static Query getNamedQuery(Session em, String namedQueryString, List<SearchItem> restrictions) {
		final HashMap<String, Object> hashMap = new HashMap<String, Object>();

		if (!CollectionUtils.isEmpty(restrictions)) {
			for (SearchItem item : restrictions) {
				if ((item.getValue() != null && StringUtils.isNotBlank(String.valueOf(item.getValue())))) {
					hashMap.put(item.getKey(), item.getValue());
				}
			}
		}

		Query query = em.getNamedQuery(namedQueryString);
		for (String key : hashMap.keySet()) {
			query.setParameter(key, hashMap.get(key));
		}
		
		return query;
	}

}
